package web.util;

import java.util.Map;

public class DaoUtil {
    /* 分页-模糊查询语句组合 */
    public static String getFuzzyQueryPageSql(String tableName, Map<String, Object> filter, int pageNum, int pageSize) {
        StringBuffer sql = new StringBuffer("select * from " + tableName);
        arristSql(sql, filter);
        sql.append(" limit " + (pageNum - 1) * pageSize + ", " + pageSize);
        return sql.toString();
    }
    /* 总纪录数-模糊查询语句组合 */
    public static String getFuzzyQueryCountSql(String tableName, Map<String, Object> filter) {
        StringBuffer sql = new StringBuffer("select count(*) from " + tableName);
        arristSql(sql, filter);
        return sql.toString();
    }

    public static void arristSql(StringBuffer sql, Map<String, Object> filter) {
        if (null != filter && !filter.isEmpty()) {
            sql .append(" where ");
            int count = 0;
            for (Map.Entry<String, Object> entry : filter.entrySet()) {
                sql.append(entry.getKey() + " like '%" + entry.getValue() + "%'");
                if (count != filter.size() - 1) {
                    sql.append(" and ");
                }
                count++;
            }
        }
    }
}
